/*
 * @Author: your name
 * @Date: 2021-04-05 01:37:20
 * @LastEditTime: 2021-04-05 03:13:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \undefinedd:\programe\tool\convert.js
 */
const fs = require('fs');
const path = require('path');
const os = require('os');

// var minimist = require('minimist');
// var args = minimist(process.argv.slice(2));

// var args = process.argv.slice(2);
// console.log(args[0]);
// console.log(args[1]);

// sync version
function walkSync(currentDirPath, callback) {    
    fs.readdirSync(currentDirPath).forEach(function (name) {
        var filePath = path.join(currentDirPath, name);
        var stat = fs.statSync(filePath);
        if (stat.isFile()) {
            callback(filePath, name, stat);
        } else if (stat.isDirectory()) {
            walkSync(filePath, callback);
        }
    });
}

// console.log(__dirname);

const targetDir = __dirname + "/sdcc";

const sfrReg = /sfr[\s]+([\w]+)[\s]+=[\s]+(0x\w\w)[\s]*;(.*)/;
const sbitReg = /sbit[\s]+([\w]+)[\s]+=[\s]+(\w+)\^(\w+);(.*)/;
const portReg = /^P\d\d$/
walkSync(__dirname +"/keil", function(filePath, name, stat) {
    // console.log(filePath, name, path.basename(filePath))
    var data=fs.readFileSync(filePath,"utf8");
    // console.log(data);
    var lines = data.split("\n")
    // var newFile = "//converte keil to sdcc" + os.EOL;
    var newFile = "//converte keil to sdcc\n";
    var map = {};
    var newLine = "";
    lines.forEach((line)=>{
        var match = line.match(sfrReg);
        if (match)
        {                        
            newLine = "__sfr __at (" + match[2] + ") " + match[1] + ";";
            if (match[3]) {
                newLine += match[3];
            }
            map[match[1]] = Number.parseInt(match[2]);
            // newFile += newLine + os.EOL;
            newFile += newLine + "\n";
        } else if (match = line.match(sbitReg)) {
            var value = map[match[2]] + Number.parseInt(match[3]);
            var varname = match[1];
            var hexValue = "0x" + value.toString(16).toUpperCase();
            if (portReg.test(match[1])) {
                // varname = match[2]+"_" + match[3];
                newLine = "__sbit __at (" + hexValue + ") " + match[2]+ "_" + match[3] + ";";
                // 加入8051.h风格port定义
                newFile += newLine + "\n";
            } 
            // else {
            //     varname = match[1];
            // }
            // 
            newLine = "__sbit __at (" + hexValue + ") " + varname + ";";
            if (match[4]) {
                newLine += match[4];
            }
            // newFile += newLine + os.EOL;
            newFile += newLine + "\n";
        }
        else 
        {
            // newFile += line + os.EOL;
            newFile += line + "\n";
        }
    })

    if (!fs.existsSync(targetDir)) {
        fs.mkdirSync(targetDir);
    }

    var targetFileName = path.join(targetDir,name);
    fs.writeFileSync(targetFileName, newFile, "utf8");
    console.log("convert", filePath, "to", targetFileName);
});